package com.net.xpay.common.manager.impl.stat;

import com.google.common.base.Preconditions;
import com.net.xpay.common.manager.stat.UserSafePayTransStatManager;
import com.net.common.dao.BasicMapper;
import com.net.common.dao.Page;
import com.net.common.manager.AbstractManager;
import com.net.xpay.common.dao.stat.UserSafePayTransStatMapper;
import com.net.xpay.common.domain.stat.UserSafePayTransStat;
import com.net.xpay.common.model.UserSafePayTransStatMonthModel;
import com.net.xpay.common.model.UserSafePayTransStatSumModel;
import com.net.xpay.core.model.UserSafePayTransStatSearchModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;


@Component
public class UserSafePayTransStatManagerImpl extends AbstractManager<UserSafePayTransStat> implements UserSafePayTransStatManager {

    @Autowired
    private UserSafePayTransStatMapper mapper;


    @Override
    protected BasicMapper<Long, UserSafePayTransStat> getMapper() {
        return mapper;
    }

    @Override
    public UserSafePayTransStat findByDayAndUserId(LocalDate day, Long userId) {
        Preconditions.checkArgument(day != null && userId != null);
        UserSafePayTransStat condition = new UserSafePayTransStat();
        condition.setDay(day);
        condition.setUserId(userId);
        return getByCondition(condition);
    }

    @Override
    public List<UserSafePayTransStat> search(UserSafePayTransStatSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        Page page = new Page(searchModel);
        page.addOrder("id", "desc");
        return nonEmptyList(mapper.search(searchModel, page));
    }

    @Override
    public long searchCount(UserSafePayTransStatSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        return mapper.searchCount(searchModel);
    }

    @Override
    public List<UserSafePayTransStatMonthModel> sumMonthUserSafePayTransStat(Integer year, Integer month, Long userId) {
        Preconditions.checkArgument(year != null && month != null && userId != null);

        List<UserSafePayTransStatMonthModel> userSafePayTransStatMonthModels = mapper.sumMonthUserSafePayTransStat(year, month, userId);
        if (userSafePayTransStatMonthModels == null) {
            return new ArrayList<>();
        }
        return userSafePayTransStatMonthModels;
    }

    @Override
    public UserSafePayTransStatSumModel sumUserSafePayTransStat(UserSafePayTransStatSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);

        return mapper.sumUserSafePayTransStat(searchModel);
    }

    @Override
    public List<UserSafePayTransStat> list(UserSafePayTransStatSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);

        return nonEmptyList(mapper.list(searchModel));
    }
}
